package com.jiang.mysportsnews.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.SlidingPaneLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.jiang.mysportsnews.R;
import com.jiang.mysportsnews.fragment.EventFragment;
import com.jiang.mysportsnews.fragment.NewsFragment;
import com.jiang.mysportsnews.fragment.TopLineFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {

    private NavigationView navigationView;
    private RadioGroup radioGroup;
    private NewsFragment newsFragment;
    private TopLineFragment topLineFragment;
    private EventFragment eventFragment;
    private SlidingPaneLayout slidingPaneLayout;
    private List<Fragment> list = new ArrayList<>();
    private int lock = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
    }

    private void init() {

        findViewById(R.id.user_iv).setOnClickListener(this);
        findViewById(R.id.video_iv).setOnClickListener(this);
        navigationView = ((NavigationView) findViewById(R.id.navigation));
        navigationView.setNavigationItemSelectedListener(this);
        navigationView.setItemIconTintList(null);
        navigationView.getHeaderView(0).findViewById(R.id.loginOrregister_btn).setOnClickListener(this);


        slidingPaneLayout = ((SlidingPaneLayout) findViewById(R.id.sliding));

        radioGroup = ((RadioGroup) findViewById(R.id.radiogroup));
        radioGroup.setOnCheckedChangeListener(this);

        newsFragment = new NewsFragment();
        topLineFragment = new TopLineFragment();
        eventFragment = new EventFragment();
        list.add(newsFragment);
        list.add(topLineFragment);
        list.add(eventFragment);
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.container, list.get(0))
                .commit();
    }


    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkId) {
        switch (checkId) {
            case R.id.news_btn:
                changefragment(0);
            case R.id.topline_btn:
                changefragment(1);
            case R.id.event_btn:
                changefragment(2);
        }

    }

    private void changefragment(int i) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        Fragment target = list.get(i);
        Fragment before = list.get(lock);
        if (target.isAdded()) {
            transaction.show(target).hide(before).commit();
        } else {
            transaction.add(R.id.container, target).hide(before).commit();
        }
        lock = i;
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        Intent intent = new Intent();
        switch (item.getItemId()) {
            case R.id.item1:
                intent.setClass(this, CommunityActivity.class);
                break;
            case R.id.item2:
                intent.setClass(this, MessageActivity.class);
                break;
            case R.id.item3:
                intent.setClass(this, SubscribleActivity.class);
                break;
            case R.id.item4:
                intent.setClass(this, CollectActivity.class);
                break;
            case R.id.item5:
                intent.setClass(this, FeedbackActivity.class);
                break;
            case R.id.item6:
                intent.setClass(this, ConfigActivity.class);
                break;
        }
        slidingPaneLayout.closePane();
        startActivity(intent);
        return true;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.user_iv:
                slidingPaneLayout.openPane();
                break;
            case R.id.video_iv:
                Toast.makeText(this, "video", Toast.LENGTH_SHORT).show();
                break;
            case R.id.loginOrregister_btn:
                startActivity(new Intent(this,LoginActivity.class));
                break;
        }
    }
}
